<?php
namespace app\api\controller;
use app\common\model\Follow as FollowModel;

class Follow extends Base
{
    protected $ff,$redis,$valid,$noNeedLogin = [];

    public function _initialize()
    {
        $this->redis = new \Redis(); //redis操作
        $this->ff = new FollowModel();//mysql操作
        $this->redis->connect('127.0.0.1', 6379);
        $this->valid = new Validate();    //验证器
        parent::_initialize();
    }
    public function addFollow()  //添加关注
    {
        $data =$this->request->param();
        $uid =$this->auth->id;
        $user_model = new \app\common\model\User();
        $data['fan_id'] = $uid;
        $user_info = $user_model->field('id,avatar,nickname,bio')->where('id',$data['follow_id'])->find();
        if(!$user_info){
            $this->error('用户已被注销');
        }
        if(!isset($data['follow_id']) || !$data['follow_id']){
            $this->error('参数错误');
        }
        $info = db('user')->where('id',$data['follow_id'])->find();
        if(!$info){
            $this->error('操作失败');
        }
        $res = $this->ff->_addFollow($data);
        if(!$res){
            $this->error('操作失败');
        }
        $this->success('关注成功','',200);
    }

    public function deleteFollow()  //取消关注
    {
        $data = $this->request->param();
        $data['fan_id'] = $this->auth->id;
        if(!isset($data['follow_id']) || !$data['follow_id']){
            $this->error('参数错误');
        }
        $info = db('user')->where('id',$data['follow_id'])->find();
        if(!$info){
            $this->error('用户已被注销');
        }
        $res = $this->ff->_deleteFollow($data);
        if(!$res){
            $this->error('操作失败');
        }
        $this->success('取关成功','',200);
    }

    public function myFanNum($need=null,$user_id=null)//关注粉丝个数
    {
        $uid  = $user_id?$user_id:$this->auth->id;
        $num = $this->myNum('_MyFan',$uid,'粉丝数',$need);
        return $num;
    }

    public function myFollowNum($need=null,$user_id=null)//关注偶像个数
    {
        $uid  = $user_id?$user_id:$this->auth->id;
        $num = $this->myNum('_MyFollow',$uid,'偶像数',$need);
        return $num;
    }

    public function myFanList($need=null,$user_id=null)//粉丝列表
    {
         $info=[];
//         $uid  = $this->request->param('user_id')?$this->request->param('user_id'):$this->auth->id;
         $uid  = $user_id?$user_id:$this->auth->id;
         $info = $this->myList('_MyFan',$uid,'粉丝列表',$need);
         if($info){
             foreach ($info as &$v){
                 $v['is_hg'] = 0;
                 //获取到我有没有关注这个人
                 if($this->redis->zScore($uid.'_MyFollow',$v['id'])){
                     $v['is_hg'] = 1;
                 }
             }
         }
         return $info;
    }

    public function myFollowList($need=null,$user_id=null)//偶像列表
    {
        $info=[];
        $where= [];
        $like_name = $this->request->get('nickname','');
        if($like_name){
            $where['nickname']=['like',"%$like_name%"];
        }
        $uid  = $user_id?$user_id:$this->auth->id;
//        $uid  = $this->request->param('user_id')?$this->request->param('user_id'):$this->auth->id;
        $info = $this->myList('_MyFollow',$uid,'偶像列表',$need,$where);
        return $info;
    }

    /**
     * 用户列表
     *
     * @param $type    //偶像/粉丝列表  _MyFollow / _MyFan
     * @param $request //Request请求类
     * @param $msg     //成功返回的信息
     * @param $need    //是否只返回数组
     * @return array
     */
    public function myList($type,$uid,$msg,$need=null,$where=[])//列表
    {
        $this->is_num($uid);
        $nums = $this->redis->zrange($uid.$type,0, -1);//返回user_id数组
        if($need){
            return $nums;
        } //只返回user id数组
        $where['id']=['in',$nums];
        $userModel =new \app\common\model\User();
        $res = $userModel->where($where)->field('avatar,id,nickname')->select();
        $res = json_decode(json_encode($res),true);
        if($res){
            foreach ($res as &$v){
                $v['is_hg'] = 0;
                //获取到我有没有关注这个人
                if($this->redis->zScore($uid.'_MyFollow',$v['id'])){
                    $v['is_hg'] = 1;
                }
            }
        }
        $this->success('获取成功',$res,200);
    }

    /**
     * 用户数量
     *
     * @param $str    //偶像/粉丝数量  _MyFollow / _MyFan
     * @param $request //Request请求类
     * @param $msg     //成功返回的信息
     * @return array
     */
    public function myNum($str,$uid,$msg,$need=null) //数量
    {
        $this->is_num($uid);
        $num = $this->redis->zCard($uid.$str); //redis获取
        if($need) {return $num;}
        $this->success('获取成功',$num,200);
    }

    public function is_num($id){
        if (!is_numeric($id)){
            $this->error('参数不正确');
        }
    }
}